package com.work.csms.mapper;

import com.work.csms.dao.Notice;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NoticeMapper {

    @Select({"<script>" +
            "SELECT n.*,u.name as userName FROM notice n left join user u on n.userid = u.userid" +
            " where 1 = 1" +
            "<if test=\"title != null and title != '' \"> " +
            " AND n.title like concat('%',#{title},'%')" +
            "</if>" +
            " limit #{offset} , #{size}" +
            "</script>"})
    List<Notice> page(String title, Integer offset, Integer size);

    @Select({"<script>" +
            "SELECT * FROM notice " +
            " where 1 = 1" +
            "<if test=\"statu != null and statu != '' \"> " +
            " AND statu = #{statu}" +
            "</if>" +
            " order by date desc" +
            "</script>"})
    List<Notice> list(Notice notice);

    @Select({"<script>" +
            "SELECT count(0) FROM notice " +
            "where 1 = 1" +
            "<if test=\"title != null and title != '' \"> " +
            " AND title like concat('%',#{title},'%')" +
            "</if>" +
            "</script>"})
    Integer count(String title);

    @Select("SELECT * FROM notice WHERE id=#{id}")
    Notice getNoticeById(Integer id);


    @Update({"<script>" +
            "update notice set " +
            "<if test=\"userid != null \"> " +
            " userid = #{userid}," +
            "</if>" +
            "<if test=\"title != null and title != '' \"> " +
            " title = #{title}," +
            "</if>" +
            "<if test=\"notice != null and notice != '' \"> " +
            " notice = #{notice}," +
            "</if>" +
            "<if test=\"statu != null and statu != '' \"> " +
            " statu = #{statu}," +
            "</if>" +
            " id = #{id}" +
            " where id = #{id}" +
            "</script>"})
    void update(Notice notice);

    @Insert("INSERT INTO notice(userid, title, notice, statu) VALUES " +
            "(#{userid}, #{title}, #{notice}, #{statu})")
    void save(Notice notice);

    @Delete("delete from notice where id = #{id} ")
    void deleted(Integer id);
}
